package com.fzf.org.interceptor;

import com.fzf.org.exception.BizException;
import com.fzf.org.exception.BizExceptionTypeEnum;
import com.fzf.org.toolkit.BasicContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
@RequiredArgsConstructor
public class AdminIdentityInterceptor implements HandlerInterceptor {

    private static final long ADMIN_ROLE = 2L;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(BasicContext.getBasicRoleId() == ADMIN_ROLE){
            return true;
        }else {
            throw new BizException(BizExceptionTypeEnum.AUTH_ERROR); // 未认证授权获取token
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 不需要渲染试图
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
